<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>编辑商品</title>
    <script src="__INDEX__/store/js/mui.min.js"></script>
	<link href="__INDEX__/store/css/mui.min.css" rel="stylesheet" />
	<script rel="stylesheet" src="__INDEX__/store/plugin/crop/croppie.min.js"></script>
	<link rel="stylesheet" href="__INDEX__/store/css/iconfont.css" />
	<link rel="stylesheet" href="__INDEX__/store/css/style.css" />
	<link rel="stylesheet" href="__INDEX__/store/css/member.css" />
	<link rel="stylesheet" href="__INDEX__/store/css/picker.css" />
	<link rel="stylesheet" href="__INDEX__/store/css/poppicker.css" />	
	<link rel="stylesheet" href="__INDEX__/store/css/datepicker3.css" />
	<link rel="stylesheet" href="__INDEX__/store/css/font-awesome.min.css" />
	<link rel="stylesheet" href="__INDEX__/store/css/bootstrap.min.css" />
<style type="text/css">
 .grzl_toux{position: relative;}
 .layui-form-label {
    float: left;
    display: block;
    padding: 9px 15px;
    width: 20%;
    font-weight: 400;
    text-align: right;
}
 *{
 	 -webkit-user-select:text;
  }
</style>
</head>
<body>
	<header class="mui-bar mui-bar-nav" style="position: fixed;">
	    <a class="mui-icon mui-icon-left-nav mui-pull-left" href="JavaScript:history.go(-1)"></a>
	    <h1 class="mui-title">编辑商品</h1>
	    <a class="mui-pull-right del" href="javascript:del_goods();" style="padding: 10px;font-size: 16px;">删除</a>
	</header>
	<div class="mui-content " style="margin-top: 20px;">
	   	<div class="gerenzl_nr">
	   		 <form id="my_form" class="layui-form update_form" action="{:url('shop/goods_edit')}" method="post">
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 30%;">商品编码</label>
					<div class="layui-input-block update_shurk">
						<input type="text" style="width: 65%;" name="goods_no" lay-verify="goods_no" value="{$goods_info.goods_no}" placeholder="商品编码" autocomplete="off" class="layui-input">
						<input type="hidden" name="id" value="{$goods_info.goods_id}">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 30%;">商品名称</label>
					<div class="layui-input-block update_shurk">
						<input type="text" style="width: 65%;" name="goods_name" lay-verify="required" value="{$goods_info.goods_name}" placeholder="商品名称" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item update_scbtn">
					<label class="layui-form-label" style="width: 30%;">上传</label>
					<div class="layui-upload" style="margin-left: 20%;">
					  <button type="button" class="layui-btn" id="test1">上传图片</button>
					  <div class="layui-upload-list" style="margin-left: 10%; width: 200px;height: 150px;" id='cut_img'>
					    <img class="layui-upload-img" style="width: 200px;height: 150px;" id="demo1" src="{$goods_info.thumb}">
					    <p id="demoText"></p>
					  </div>  
					</div>   
					<input type="hidden" name="thumb" id='thumb' value="{$goods_info.thumb}" />
				</div> 
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 32%;padding: 9px 3px;">平台一级分类</label>
					<div class="layui-input-block" style="float: left; margin-left: 0;width: 50%;">
						<select name="cate_id" id='cate_id' lay-verify="required" lay-filter="sel_1">
							{volist name="platform_1" id="vo"}
								{if $vo.id == $goods_info.cate_id}
									<option value="{$vo.id}">{$vo.type_name}</option>
								{/if}
							{/volist}
							{volist name="platform_1" id="vo"}
								{if $vo.id != $goods_info.cate_id}
									<option value="{$vo.id}">{$vo.type_name}</option>
								{/if}
							{/volist}
						</select>
					</div>
					<label class="layui-form-label" style="width: 32%;">平台二级分类</label>
					<div class="layui-input-block" style="float: left; margin-left: 0;width: 50%;">
						<select name="cate2_id" id='cate2_id' lay-verify="required" lay-filter="sel_2">
							{volist name="platform_2" id="vo"}
								 {volist name="vo" id="item" key="k"}
								 		{if $item.id == $goods_info.cate2_id}
								 			<option value="{$item.id}">{$item.type_name}</option>
								 		{/if}
							    {/volist}
							{/volist}
							{volist name="platform_2" id="vo"}
								 {volist name="vo" id="item" key="k"}
								 		{if $item.id != $goods_info.cate2_id}
								 			<option value="{$item.id}">{$item.type_name}</option>
								 		{/if}
							    {/volist}
							{/volist}
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 32%;">商品分类</label>
					<div class="layui-input-block" style="float: left; margin-left: 0;width: 50%;">
						<select name="trade_id" lay-verify="required">
							{volist name="agent_type" id="vo"}
								{if $vo.id == $goods_info.trade_id}
									<option value="{$vo.id}">{$vo.type_name}</option>
								{/if}
							{/volist}
							{volist name="agent_type" id="vo"}
								{if $vo.id != $goods_info.trade_id}
									<option value="{$vo.id}">{$vo.type_name}</option>
								{/if}
							{/volist}
						</select>
					</div>
				</div>
				
				<!--<div class="layui-form-item">
					<label class="layui-form-label" style="width: 32%;">归属门店</label>
					<div class="layui-input-block" style="float: left; margin-left: 0;width: 50%;">
						<select name="chainstore_id" lay-verify="required">
							{volist name="storelist" id="vo"}
								{if $vo.id == $goods_info.chainstore_id}
									<option value="{$vo.id}">{$vo.store_name}</option>
								{/if}
							{/volist}
							<option value="0">通用</option>
							{volist name="storelist" id="vo"}						
								{if $vo.id != $goods_info.chainstore_id}
									<option value="{$vo.id}">{$vo.store_name}</option>
								{/if}
							{/volist}
						</select>
					</div>
				</div>-->
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 32%;">成本价</label>
					<div class="layui-input-block update_shurk">
						<input type="text"  style="width: 32%;float: left;" name="cost_price" id="cost_price" value="{$goods_info.cost_price}"  placeholder="成本价"  class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')">
						<span style="line-height: 39px;">元</span>
					</div> 
				</div>			
				<!--<div class="layui-form-item">
					<label class="layui-form-label" style="width: 32%;">零售价</label>
					<div class="layui-input-block update_shurk">
						<input onkeyup="keyup(this)" type="text"  style="width: 32%;float: left;" name="sale_price" id="sale_price" value="{$goods_info.sale_price}"  placeholder="销售价"  class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')">
						<span style="line-height: 39px;float: left;">元，送</span>
						<input onkeyup="keyups(this)" type="text" style="width: 25%;float: left;"  name="zs_price" value="{$goods_info.zs_price}" required lay-verify="required" placeholder="赠送" autocomplete="off" class="layui-input">
						<label class="layui-form-label" style="width: unset;padding: 9px 0px;">鼓励金</label>
					</div> 
				</div>-->
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 32%;">会员价</label>
					<div class="layui-input-block update_shurk hui">
						<input onkeyup="keyup(this)" type="text"  style="width: 32%;float: left;" name="aprice" id="aprice" value="<?php echo $goods_info['sale_price'] - $goods_info['dk_price'] ?>"  placeholder="会员价"  class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')">
						<span style="line-height: 39px;float: left;">元, &nbsp;+</span>
						<input onkeyup="keyup(this)" style="width: 25%;float: left;" readonly="readonly" type="text" name="dk_price" id="dk_price" value="{$goods_info.dk_price}" required lay-verify="required" placeholder="抵扣" autocomplete="off" class="layui-input">
						<label class="layui-form-label" style="width: unset;padding: 9px 0px;">鼓励金</label>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 30%;">库存</label>
					<div class="layui-input-block update_shurk">
						<input type="text" style="width: 25%;float: left;" name="stock"  placeholder="库存" value="{$goods_info.stock}" lay-verify="required"  class="layui-input" onkeyup="value=value.replace(/[^\d\.]/g,'')" onblur="value=value.replace(/[^\d\.]/g,'')">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 30%;">计量单位</label>
					<div class="layui-input-block update_shurk" style="margin-left: 0px;width: 20%;float: left;">
						<select name="unit" style="float: left;width: 30%;">
							<option value=""></option>
							{volist name="$Think.config.units" id="vo"}
								<option {if $goods_info.unit==$vo}selected="selected" {/if} value="{$vo}">{$vo}</option>
							{/volist}
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 30%;">是否推荐</label>
					<div class="layui-input-block">
						<input type="radio" name="is_recommend" value="2" title="是" {if $goods_info.is_recommend == 2}checked{/if}>
						<input type="radio" name="is_recommend" value="1" title="否" {if $goods_info.is_recommend == 1}checked{/if}>
					</div>
				</div>			
				<div class="layui-form-item layui-form-text">
					<label class="layui-form-label" style="width: 30%;">商品详情</label>
				</div>
				<div class="layui-form-item layui-form-text" style="width: 95%;margin-left: 3%;z-index: 100;position: relative;">
					<div id="div1" class="toolbar" style="background-color: #d4cece;"></div>
		    		<div id="div2" class="text" style="height: 450px;background-color: #f7f7f7;" ></div>
		    		<textarea name="content" required lay-verify="required" id="content" style="display: none;">{$goods_info["content"]}</textarea>
				</div>
				<div class="layui-form-item update_btnbox" style="text-align: center;margin-left:unset">
					<div class="layui-input-block" style="    max-width: unset;float: unset;">
						<button onclick="content_vale()" id='change_but' type="button" lay-submit class="layui-btn" lay-filter="demo1">立即提交</button>
						<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
				</div>
			</form>
	   	</div>
	   	<input type="hidden" id="store_id" >
	</div>

</body>
</html>
{include file="public/footer" /}
<script type="text/javascript" src="__PUBLIC__/static/release/wangEditor.min.js"></script>
<script type="text/javascript" src='__PUBLIC__/static/js/html2canvas.js'></script>
<script>
	$(".hui input[type='text']").change(function(){
		if($("#aprice")[0].value==0){
			layer.msg('会员价不能为0');
			return;
		}
		$("#dk_price").val(parseFloat(($("#cost_price")[0].value)-parseFloat($("#aprice")[0].value)).toFixed(2));
	})
	//二级分类
	var platform_2 = <?php echo json_encode($platform_2);?>;
	
	//富文本编辑器
    var E = window.wangEditor;
    var editor1 = new E('#div1', '#div2');
    editor1.customConfig.uploadImgServer="wang_upload"; 
    editor1.customConfig.uploadImgHooks = {
    	customInsert: function (insertImg, result, editor) {
        var url = result.src;
        insertImg(url)
    	}
    }
    editor1.customConfig.menus = [
        'source',
        '|',
        'bold',
        'underline',
        'italic',
        'strikethrough',
        'eraser',
        'forecolor',
        'bgcolor',
        '|',
        'quote',
        'fontfamily',
        'fontsize',
        'head',
        'unorderlist',
        'orderlist',
        'alignleft',
        'aligncenter',
        'alignright',
        '|',
        'link',
        'unlink',
        'table',
        'emotion',
        '|',
        'img',
        'video',
        'location',
        'insertcode',
        '|',
        'undo',
        'redo',
        'fullscreen'
     ];
    editor1.create();
    editor1.txt.html($('#content').val());
    
    function keyup(obj) {
	obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
	obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
	obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
	obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	}
    
    function keyups(obj) {
    	var sale_price = $('#sale_price').val();
    	if(parseFloat(sale_price)<parseFloat(obj.value)){
    		obj.value = sale_price;
    	}
    	
	obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
	obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
	obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
	obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	}
    function content_vale(){
    	$("#content").val(editor1.txt.html());
    }
    
    //提交表单
	function set_content(){
  		 $.ajax({
             	type:"post",
                url:"{:url('agent/goods_edit')}",
                data: $('#my_form').serialize(),//表单数据
                success:function(data){
                    if(data.code==200){
                    	layer.msg(data.msg, {
						  icon: 1,
						  time: 1500 //2秒关闭（如果不配置，默认是3秒）
						}, function(){
							location.href="{:url('agent/goods_list')}";
						});
                    }else{
                    	layer.msg('添加失败！');//保存成功提示
                    }
                }
        });
  		
  	}
	
	
	//图片上传
	layui.use(['form','upload'], function(){
  		var $ = layui.jquery,upload = layui.upload,form = layui.form;
  		
  		
  		//普通图片上传
		var uploadInst = upload.render({
			elem: '#test1'
		    ,auto: false //选择文件后不自动上传
		    ,choose: function(obj){
			    //将每次选择的文件追加到文件队列
			    var files = obj.pushFile();
			    
			    //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
			    obj.preview(function(index, file, result){
			    	
			    	$('#demo1').attr('src', result); //图片链接（base64）
			    	
			    	html2canvas($('#cut_img'), {
					 onrendered: function (canvas) {
					    var base_url = canvas.toDataURL(); //base64数据
					   	var sendData = base_url.substring(base_url.indexOf(";base64,")+8);
				    	$.post("{:url('agent/index/upload')}",{value:sendData},function(data){ 
		                            if(data.code == '200'){  
		                            	$('#thumb').val(data.url);
		                            }else{
		                                layer.msg(data.msg);
		                            }
	                    });
					   }
					});
			    });
			}
		    ,url: "{:url('index/upload')}"
		    ,accept:'images'
		});
	  
		//监听下拉框
		form.on('select(sel_1)', function(data){
			
			var cate_id = data.value;
			
			var cate2_type = platform_2[cate_id];
			var select_htm = '';
			
			if(cate2_type && cate2_type.length<1){
				select_htm = "<option value=''></option>";
			}else{
				for(var i=0;i<cate2_type.length;i++){
					select_htm += "<option value='"+cate2_type[i].id+"'>"+cate2_type[i].type_name+"</option>";
				}
			}
			
			$('#cate2_id').empty();
			$('#cate2_id').append(select_htm);
			form.render('select');
			
			
		});  
	  
	  	//监听提交
		form.on('submit(demo1)', function(data){
			$('#change_but').addClass("layui-btn-disabled");
			set_content();
			
		    return false;
		});
	})
	
	function del_goods(){
		layer.confirm('确定删除？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
		  $.ajax({
				url: "{:url('agent/del_goods')}",
				data: {
					goods_id:"{$goods_info['goods_id']}"
				},
				success: function(data) {
					layer.msg(data.msg, {
										time: 2000
								}, function(){
					    			location.href="{:url('agent/goods_list')}";
							});
				}
			});
			layer.close();
		});
	}
</script>